from PIL import Image
from selenium import webdriver
import time,unittest
from selenium.webdriver.common.by import By
from Day_2211A.Unit_09.mysql import Mysql
from selenium.webdriver.common.action_chains import ActionChains


class A3mall(unittest.TestCase):
    def setUp(self):
        self.sql = Mysql('10.59.9.12', 'root', 'yidong666', 'a3mall')

    def tearDown(self):
        self.sql.close()

    amount = None
    price = None
    freight = None
    num = None
    ddh1 = None

    def test_01(self):
        self.driver = webdriver.Edge()
        self.driver.maximize_window()
        self.driver.get('http://10.59.9.12:7001/')
        time.sleep(1)
        # 点击登录
        self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').click()
        time.sleep(2)
        # 输入用户名密码
        self.driver.find_element(By.NAME, 'username').send_keys('15523615757')
        self.driver.find_element(By.NAME, 'password').send_keys('123456')
        # # 获取验证码
        # self.driver.save_screenshot('datu.png')
        # yzm = self.driver.find_element(By.XPATH,'/html/body/div[4]/div/div[1]/div[2]/form/div[3]/span[3]/img')
        # location = yzm.location
        # size = yzm.size
        # lift = location['x']
        # top = location['y']
        # right = lift+size['width']
        # bottom = top+size['height']
        # image1 = Image.open('datu.png')
        # image2 = image1.crop((lift*1.5,top*1.5,right*1.5,bottom*1.5))
        # image2.save('yzm.png')
        # print(image2)
        # # 调用获取验证码的方法
        # dm = Dama('lumieng','lj070130','lj070130')
        # yzmdm = open('yzm.png','rb').read()
        # jirguo = dm.PostPic(yzmdm,1902)
        # print(jirguo)
        # result = jirguo['pic_str']
        # 填写验证码
        # self.driver.find_element(By.XPATH,'//*[@id="theForm"]/div[3]/span[2]/input').send_keys(result)
        # time.sleep(1)

        # 点击登录
        self.driver.find_element(By.XPATH, '/html/body/div[4]/div/div[1]/div[2]/form/div[5]/a').click()
        time.sleep(8)
        # 断言，账号已登录
        dy1 = self.driver.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[1]/a').text
        print(dy1)
        self.assertIn('15523615757', dy1)
        # 账户余额
        money1 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div[1]/div[2]/div[2]/ul/li[4]/span').text
        money2 = money1.split(' ')
        A3mall.amount = float(money2[1])
        print(A3mall.amount)
        # 输入要搜索的内容
        self.driver.find_element(By.CSS_SELECTOR, '#searchForm > div.header-search > input').send_keys('小米')
        time.sleep(1)
        # 搜索
        self.driver.find_element(By.CLASS_NAME, 'search-input-btn').click()
        time.sleep(1)
        # 选择商品
        self.driver.find_element(By.XPATH, '/html/body/div[5]/div[2]/div[2]/div[1]/ul/li[1]/div[1]/span/a/img').click()
        time.sleep(2)
        dy2 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div/div[2]/form/div/div[1]/span').text
        print(dy2)
        self.assertIn('小米',dy2)
        # 获取商品价格
        price = self.driver.find_element(By.ID, 'sell-price-box').text
        A3mall.price = float(price)
        print(A3mall.price)
        # 断言商品价格是否与数据库价格一样
        dy3 = self.driver.find_element(By.ID, 'sell-price-box').text
        print(dy3)
        price = self.sql.chaxun('select sell_price from mall_goods where id = 26')
        A3mall.price = str(price[0][0])
        print(A3mall.price)
        self.assertEqual(dy3, A3mall.price)
        # 定位立即购买
        self.driver.find_element(By.ID, 'buy-cart').click()
        time.sleep(2)
        self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
        time.sleep(1)
        # 断言商品数量是否正确
        dy3 = self.driver.find_element(By.XPATH,'/html/body/div[4]/form/div[4]/div[1]/table/tbody/tr[2]/td[3]').text
        print(dy3)
        self.assertEqual('3',dy3)
        # 获取运费
        num = self.driver.find_element(By.ID,'real-freight').text
        A3mall.freight = float(num)
        print(A3mall.freight)
        # 定位下一步按钮
        self.driver.find_element(By.ID, 'order-button').click()
        time.sleep(5)
        dy4 = self.driver.find_element(By.CLASS_NAME, 'succe-ico').text
        print(dy4)
        self.assertEqual("您的订单支付成功", dy4)
        # 点击查看订单
        self.driver.find_element(By.XPATH,'/html/body/div[4]/div[3]/p/a').click()
        time.sleep(2)
        # 获取订单号
        A3mall.ddh1 = self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[2]/td[2]').text
        print(A3mall.ddh1)
        # 点击退款
        self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[2]/table[2]/tbody/tr[6]/td[2]/div[1]/a').click()
        time.sleep(2)
        # 输入退款条件
        self.driver.find_element(By.XPATH,'/html/body/div[5]/div[2]/div/div[3]/form/table/tbody/tr[1]/td/span/textarea').send_keys('这个包一点都不肿')
        # 点击提交
        self.driver.find_element(By.ID,'user-info-btn').click()
        time.sleep(5)
        self.driver.close()
        self.driver.quit()

    def test_02(self):
        # 断言
        money = self.sql.chaxun('select amount from mall_users where username = "15523615757"')
        money1 = float(money[0][0])
        print(money1)
        self.assertEqual(float(A3mall.amount)-float(A3mall.freight), money1)
